import { createStore } from "vuex";
import cart, {
  CartState,
  CartGetters,
  CartMutations,
  CartActions,
} from "./modules/cart";
import user, {
  UserState,
  UserGetters,
  UserMutations,
  UserActions,
} from "./modules/user";

export default createStore({
  state: {},
  mutations: {},
  actions: {},
  modules: {
    user,
    cart,
  },
});

export interface RootState {
  user?: UserState;
  cart?: CartState;
}

export interface RootGetters extends UserGetters, CartGetters {}

export interface RootMutations extends UserMutations, CartMutations {}

export interface RootActions extends UserActions, CartActions {}

declare module "vuex" {
  interface RootCommit {
    <TypeKey extends keyof RootMutations>(
      type: TypeKey,
      payload?: RootMutations[TypeKey],
      options?: CommitOptions
    ): void;
    <P extends Payload>(payloadWithType: P, options?: CommitOptions): void;
  }

  export interface RootDispatch {
    <TypeKey extends keyof RootActions>(
      type: TypeKey,
      payload?: RootActions[TypeKey]["payload"],
      options?: DispatchOptions
    ): Promise<RootActions[TypeKey]["promise"]>;
    <P extends Payload>(
      payloadWithType: P,
      options?: DispatchOptions
    ): Promise<any>;
  }

  interface RootStore<S = any> extends Store<S> {
    readonly getters: RootGetters;
    commit: RootCommit;
    dispatch: RootDispatch;
  }

  export function useStore<S = RootState>(): RootStore<S>;

  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  export interface Module<S, R = RootState> {}
}
